﻿@model List<Agile.Service.Reward.SalaryColumn>
@{
    ViewBag.Title = "工资表";
}
@section header{
    <style>
        .selectBind {
            width: 100%;
            height: 30px;
            border-color: #DDDDDD;
            outline: none;
        }

        .nav-tabs li {
            cursor: pointer;
        }
    </style>
}
@if (Model.Count > 0)
{
    <div class="ui-layout" id="layout" style="height: 100%; width: 100%;">
        <div class="ui-layout-north">
            <ul class="nav nav-tabs">
                @for (var i = 0; i < Model.Count; i++)
                {
                    if (i == 0)
                    {
                        <li class="@Model[i].ID active">
                            <a id="@Model[i].ID" authorize="yes" onclick="changePage('@Model[i].ID')">@Model[i].Name</a>
                        </li>
                    }
                    else
                    {
                        <li class="@Model[i].ID">
                            <a id="@Model[i].ID" authorize="yes" onclick="changePage('@Model[i].ID')">@Model[i].Name</a>
                        </li>
                    }
                }
            </ul>
        </div>
        <div class="ui-layout-west">
            <input id="zTreesso" name="zTreesso" type="text" style="width:100%;height:25px;" placeholder="请输入关键字" />
            <ul id="itemTree" class="ztree"></ul>
        </div>
        <div class="ui-layout-center">
            <div class="topPanel">
                <div class="toolbar">
                    <div class="btn-group">
                        <a class="btn btn-primary" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span></a>
                    </div>
                    <div class="btn-group">
                        <a id="NF-add" authorize="yes" class="btn btn-primary" onclick="btn_add()"><i class="fa fa-plus"></i>添加</a>
                        <a class="btn btn-primary dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></a>
                        <ul class="dropdown-menu pull-right">
                            <li><a id="NF-GenerateSalary" authorize="yes" onclick="btn_GenerateSalary()" href="javascript:void()">生成工资表</a></li>
                            <li><a id="NF-Import" authorize="yes" onclick="btn_Import()" href="javascript:void()">导入工资表</a></li>
                            <li><a id="NF-Export" authorize="yes" onclick="btn_Export()" href="javascript:void()">导出工资表</a></li>
                        </ul>
                    </div>
                    <div class="operate">
                        <ul class="nav nav-pills">
                            <li class="first">已选中<span>1</span>项</li>
                            <li><a id="NF-edit" authorize="yes" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>修改</a></li>
                            <li><a id="NF-delete" authorize="yes" onclick="btn_delete()"><i class="fa fa-trash-o"></i>删除</a></li>
                            <li><a id="NF-Details" authorize="yes" onclick="btn_details()"><i class="fa fa-search-plus"></i>查看</a></li>
                        </ul>
                        <a href="javascript:;" class="close"></a>
                    </div>
                    <script>$('.toolbar').authorizeButton()</script>
                </div>
                <div class="search">
                    <table class="table">
                        <tr>
                            <td><div class="input-group">年：</div></td>
                            <td><div class="input-group"><select id="txt_year" name="txt_year" class="form-control" style="width: 80px;"></select></div></td>
                            <td><div class="input-group">月：</div></td>
                            <td> <div class="input-group"><select id="txt_month" name="txt_month" class="form-control" style="width: 80px;"></select></div></td>
                            <td>
                                <div class="input-group">
                                    <input id="txt_keyword" type="text" class="form-control" placeholder="名称/编号" style="width: 200px;">
                                    <span class="input-group-btn">
                                        <button id="btn_search" type="button" class="btn  btn-primary"><i class="fa fa-search"></i></button>
                                    </span>
                                </div>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
            <div class="gridPanel">
                <table id="gridList"></table>
                <div id="gridPager"></div>
            </div>
        </div>

    </div>
}
else
{
    <div class="alert alert-warning">
        警告！请移步到
        <a class="menuItem" data-id="" href="/Reward/SalaryColumn/Index" data-index="">工资公式</a>
        页面配置好，在进入本页面！
    </div>
}

@section scripts{
    <script type="text/javascript">
        var baseUrl = "/reward/Salary/";
        var keyword = ""
        var treeNodeId = "";
        var yemo = "";
        $(function () {
            $('#layout').layout();
            initControl();
        })
        function initControl() {
            yemo = ($("#txt_year").val() + '' + $("#txt_month").val());
            keyword = $("#txt_keyword").val();
            TreeOrg();
            $("#txt_year").bindSelect({
                search: false,
                url: baseUrl + "GetDateSelectJson?type=year"
            });
            $("#txt_year").val("@DateTime.Now.Year").trigger("change");
            $("#txt_month").bindSelect({
                search: false,
                url: baseUrl + "GetDateSelectJson?type=month"
            });
            $("#txt_month").val("@DateTime.Now.Month.ToString().PadLeft(2,'0')").trigger("change");

            var actid = $('.active a').attr('id');
            changePage(actid);
        }
        function TreeOrg() {
            var setting = {
                view: {
                    dblClickExpand: false,
                    showLine: true,
                    selectedMulti: false
                },
                data: {
                    key: {
                        name: "Name",
                    },
                    simpleData: {
                        enable: true,
                        idKey: "ID",
                        pIdKey: "ParentID",
                        rootPId: ""
                    }
                },
                callback: {
                    // 单击事件
                    onClick: function zTreeOnClick(event, treeId, treeNode) {
                        //console.log(treeNode)
                        treeNodeId = treeNode.ID;
                        $("#txt_keyword").val('');
                        $('#btn_search').trigger("click");
                    }
                }
            };
            $.ajax({
                url: "/rbac/Organize/GetAll",
                type: "post",
                dataType: "json",
                success: function (data) {
                    var zTreeObj = $.fn.zTree.init($("#itemTree"), setting, data);
                    fuzzySearch('itemTree', '#zTreesso', null, true); //初始化模糊搜索方法
                }
            });
        }
        function changePage(key_code) {
            $(".nav-tabs li").removeClass("active")
            $("." + key_code + "").addClass("active");

            yemo = ($("#txt_year").val() + '' + $("#txt_month").val());
            keyword = $("#txt_keyword").val();

            console.log("changePage:" + $('.active a').attr('id'));

            pageInit(yemo, treeNodeId);
        }
        //表
        function pageInit(yemo, orgId) {
            $.jgrid.gridUnload("gridList");
            var jqdata = "";
            var names = [];
            var model = [];
            //创建jqGrid组件
            $.ajax({
                url: baseUrl +"GetTableJson",
                data: { yemo: yemo, wageType: $('.active a').attr('id') },
                dataType: "json",
                async: false,
                success: function (result) {
                    console.log(result);
                    if (result == "false") {
                        $("#NF-GenerateSalary").click();
                    }
                    else if (result == "") {
                        $.modalMsg("暂无数据！！！", "error");
                        return false;
                    }
                    else {
                        jqdata = result.substring(0, result.length - 1);
                        jqdata = jqdata.split(',');
                        fields = jqdata;
                        names.push("序号");
                        model.push({
                            name: "ID", hidden: true, key: true, frozen: true
                        });
                        names.push("姓名");
                        model.push({
                            name: "UserName", width: 100, align: 'center', frozen: true
                        });
                        names.push("工号");
                        model.push({
                            name: "UserCode", width: 100, align: 'center', frozen: true
                        });
                        names.push("部门");
                        model.push({
                            name: "OrgName", width: 100, align: 'center', frozen: true
                        });
                        for (var i = 0; i < jqdata.length; i++) {
                            names.push(jqdata[i]);
                            model.push({
                                name: jqdata[i], width: 100, align: 'center'
                            });
                        }
                    }
                }
            });

            $("#gridList").dataGrid({
                url: baseUrl + 'GetSalaryTreeGridJson',
                postData: { yemo: yemo, orgId: treeNodeId, wageType: $('.active a').attr('id') },
                height: $(window).height() - 220,
                colNames: names,//jqGrid的列显示
                colModel: model,//jqGrid的行显示
                rowNum: 18,
                rowList: [18, 36, 54, 72, 90, 108],
                pager: '#gridPager',
                sortname: 'OrgName',
                sortorder: "asc",
            });
            $("#btn_search").click(function () {
                $("#gridList").jqGrid('setGridParam', {
                    url: baseUrl + "GetSalaryTreeGridJson",
                    postData: { keyword: $("#txt_keyword").val(), yemo: yemo, orgId: treeNodeId, zblx: $("#select_ZB").val() },
                }).trigger('reloadGrid');
            });
            //$("#gridList").jqGrid('setFrozenColumns');

            //$("#gridList").jqGrid('setGridParam', {
            //    url: baseUrl + "GetSalaryTreeGridJson",
            //    postData: { keyword: $("#txt_keyword").val(), yemo: yemo, orgId: orgId, zblx: $("#select_ZB").val() },
            //}).trigger('reloadGrid');

        }

        function btn_GenerateSalary() {
            if ($("#txt_year").val() == "" || $("#txt_month").val() == "") {
                $.modalMsg("暂无数据！！！", "error");
                return false;
            }
            var postData = { yemo: yemo, wageType: $('.active a').attr('id') }

            $.submitForm({
                url: baseUrl +"GenerateSalary",
                param: postData,
                success: function () {
                    $.currentWindow().$("#gridList").trigger("reloadGrid");
                }
            })
        }
        function btn_Import() {
            $.modalOpen({
                id: "Form",
                title: "工资表导入",
                url: baseUrl +"ImportForm",
                width: "600px",
                height: "600px",
                btn: [],
                callBack: function (iframeId) {
                    top.frames[iframeId].submitForm();
                }
            });
        }
        function btn_Export() {
            //导出
            $.exportForm({
                url: baseUrl + "ExportForm?keyword=" + $("#txt_keyword").val()
                    + "&yemo=" + yemo
                    + "&orgId=" + orgId
                    + "&wageType=" + $('.active a').attr('id'),
            });
        }
    </script>
}